﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarolLib.Control;
using System.Xml;
using DispatchAssist.Service;
using System.Web.Services.Protocols;
using CarolLib;

namespace DispatchAssist.AppHost
{
    [ModuleContract("8eed69bf-5ece-4d39-a809-8a082c5fd2e1", "系统管理", "签派席位管理")]
    public partial class SeatManage : MdiUserControl
    {
        AuthService authService = new AuthService();
        List<Seat> seats = new List<Seat>();
        Seat seate = new Seat();
        public SeatManage()
            : base()
        {
            InitializeComponent();

        }
        public SeatManage(XmlNode node)
            : this()
        {
        }
        private void btnCreateEdit_Click(object sender, EventArgs e)
        {
            if ((sender as Button).Tag.ToString() == "create")
            {
                CreateSeat();
            }
            else if ((sender as Button).Tag.ToString() == "edit")
            {
                EditSeat();
            }
        }

        private void EditSeat()
        {
            if (dgrdSeatList.SelectedRows == null || dgrdSeatList.SelectedRows.Count <= 0 || dgrdSeatList.SelectedRows.Count > 1)
            {
                MessageBox.Show("请选择要编辑的记录行！");
                return;
            }
            rowID = this.dgrdSeatList.CurrentRow.Index;
            Guid seatID = new Guid(System.Convert.ToString(dgrdSeatList.CurrentRow.Cells["SeatID"].Value));

            seate = seats.Find(s => s.SeatID == seatID);
            txtSeatName.Text = System.Convert.ToString(dgrdSeatList.CurrentRow.Cells["SeatName"].Value);
            txtMAC.Text = System.Convert.ToString(dgrdSeatList.CurrentRow.Cells["MAC"].Value);
            this.btnSave.Tag = System.Convert.ToString(seatID);
            this.grpEditSeat.Visible = true;
            this.btnCreate.Enabled = false;
            this.btnEdit.Enabled = false;
            this.btnDelete.Enabled = false;
        }

        private void CreateSeat()
        {
            this.grpEditSeat.Visible = true;
            this.btnSave.Tag = "create";
            txtMAC.Text = string.Empty;
            txtSeatName.Text = string.Empty;
            txtSeatName.Focus();
            rowID = this.dgrdSeatList.CurrentRow.Index;
            this.btnCreate.Enabled = false;
            this.btnEdit.Enabled = false;
            this.btnDelete.Enabled = false;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.dgrdSeatList.SelectedRows == null || dgrdSeatList.SelectedRows.Count <= 0 || dgrdSeatList.SelectedRows.Count > 1)
            {
                MessageBox.Show("请选择要删除的记录行！");
                return;
            }
            if (MessageBox.Show("确认删除此席位？", "删除确认", MessageBoxButtons.OKCancel) != DialogResult.OK)
            {
                return;
            }
            Guid seatID = new Guid(System.Convert.ToString(dgrdSeatList.CurrentRow.Cells["SeatID"].Value));
            Seat seat = new Seat();
            seat.SeatID = seatID;
            try
            {
                if (authService.DeleteSeat(seat))
                {
                    MessageBox.Show("删除成功！");
                    BindSeatList();
                }
                else
                {
                    MessageBox.Show("删除失败！");
                }
            }
            catch (SoapHeaderException soapEx)
            {
                MessageBox.Show("SoapHeader Error : " + soapEx.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error : " + ex.Message);
            }

        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            string tag = System.Convert.ToString((sender as Button).Tag);
            if (string.IsNullOrEmpty(this.txtSeatName.Text))
            {
                MessageBox.Show("请输入席位名称！");
                return;
            }
            if (string.IsNullOrEmpty(this.txtMAC.Text))
            {
                MessageBox.Show("请输入MAC地址！");
                return;
            }
            if (!SysInfoHelper.IsValidateOfMac(this.txtMAC.Text))
            {
                MessageBox.Show("MAC地址无效！");
                return;
            }

            // 查找方法放在最近位置. ccp.
            Seat seat = seats.Find(s => s.SeatName == txtSeatName.Text);
            Seat seat2 = seats.Find(s => s.MAC == txtMAC.Text);
            if (tag == "create")
            {
                if (seat != null)
                {
                    MessageBox.Show("席位名称已经存在！");
                    return;
                }
                if (seat2 != null)
                {
                    MessageBox.Show("MAC地址已经存在！");
                    return;
                }
                SaveNewSeat();
            }
            else if (!string.IsNullOrEmpty(tag))
            {
                if (seat != null && seate.SeatName != txtSeatName.Text)
                {
                    MessageBox.Show("席位名称已经存在！");
                    return;
                }
                if (seat2 != null && seate.MAC != txtMAC.Text)
                {
                    MessageBox.Show("MAC地址已经存在！");
                    return;
                }
                SaveEditSeat(tag);
            }
            this.grpEditSeat.Visible = false;
        }

        private void SaveEditSeat(string seatID)
        {
            Seat seat = new Seat();
            seat.SeatID = new Guid(seatID);
            seat.SeatName = this.txtSeatName.Text;
            seat.MAC = this.txtMAC.Text;
            try
            {
                if (authService.UpdateSeat(seat))
                {
                    MessageBox.Show("保存成功！");
                    BindSeatList();
                    this.btnCreate.Enabled = true;
                    this.btnEdit.Enabled = true;
                    this.btnDelete.Enabled = true;
                }
                else
                {
                    MessageBox.Show("保存失败！");
                }
            }
            catch (SoapHeaderException soapEx)
            {
                MessageBox.Show("SoapHeader Error : " + soapEx.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error : " + ex.Message);
            }

        }

        int rowID = -1;

        private void dgvdRoleList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if ((rowID != -1) && this.grpEditSeat.Visible && (this.dgrdSeatList.CurrentRow.Index != rowID))
            {
                if (MessageBox.Show("是否撤销当前编辑？", "编辑确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                {
                    dgrdSeatList.Rows[rowID].Selected = true;
                }
                else
                {
                    grpEditSeat.Visible = false;
                    this.btnCreate.Enabled = true;
                    this.btnEdit.Enabled = true;
                    this.btnDelete.Enabled = true;
                }
            }
        }
        private void SaveNewSeat()
        {
            Seat seat = new Seat();
            seat.SeatName = this.txtSeatName.Text;
            seat.MAC = this.txtMAC.Text;
            try
            {
                if (authService.CreateSeat(seat))
                {
                    MessageBox.Show("添加成功！");
                    BindSeatList();
                    this.btnCreate.Enabled = true;
                    this.btnEdit.Enabled = true;
                    this.btnDelete.Enabled = true;
                }
                else
                {
                    MessageBox.Show("添加失败！");
                }
            }
            catch (SoapHeaderException soapEx)
            {
                MessageBox.Show("SoapHeader Error : " + soapEx.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error : " + ex.Message);
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            grpEditSeat.Visible = false;
            this.btnCreate.Enabled = true;
            this.btnEdit.Enabled = true;
            this.btnDelete.Enabled = true;
        }
        public override void Onload()
        {
            base.Onload();
            authService.DASoapHeaderValue = AuthContext.Current.GetSoapHeader();
            try
            {
                BindSeatList();
                this.grpEditSeat.Visible = false;
                this.btnDelete.Enabled = true;
                this.btnCreate.Enabled = true;
                this.btnEdit.Enabled = true;
            }
            catch (SoapHeaderException soapEx)
            {
                MessageBox.Show("SoapHeader Error : " + soapEx.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error : " + ex.Message);
            }
        }

        private void BindSeatList()
        {
            Seat[] arrSeats = authService.GetAllSeat();
            if (arrSeats != null)
            {
                seats = arrSeats.ToList<Seat>();
                this.dgrdSeatList.AutoGenerateColumns = false;
                if (seats != null)
                {
                    dgrdSeatList.DataSource = seats;
                }
            }
        }
    }
}
